﻿using HWT;
using System.Diagnostics;

ThreadPool.SetMaxThreads(600, 600);
ThreadPool.SetMinThreads(550, 550);

int num = 0;
_ = Task.Factory.StartNew(() =>
{
    Stopwatch sw = Stopwatch.StartNew();
    while (true)
    {
        Thread.Sleep(5000);
        double speed = num / sw.Elapsed.TotalSeconds;
        Console.WriteLine($"10000个循环干活总速度={speed:#### ####.0} 次/秒");
    }
}, TaskCreationOptions.LongRunning);

HashedWheelTimer timer = new HashedWheelTimer(TimeSpan.FromMilliseconds(15), 500, -1);

async void func(int i)
{
    while (true)
    {
        await timer.Delay(0);
        // Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff} - {i}");

        await Task.Run(() =>
        {
            Thread.Sleep(100);
            Interlocked.Increment(ref num); // 干活
        });
    }
}

for (int i = 0; i < 500; i++)
{
    func(i);
}

Console.WriteLine("开始");
Console.ReadLine();
